<template>
  <el-card class="content" shadow="hover">
    <!-- 名称、编号输入框、查询、清空按钮 -->
    <div class="handel_Input">
      <el-input
        class="Input_name"
        placeholder="医院名称"
        v-model="hosname"
        clearable
      >
      </el-input>
      <el-input
        class="Input_number"
        placeholder="医院编号"
        v-model="hoscode"
        clearable
      >
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="Search"
        >查询</el-button
      >
      <el-button @click="remove">清空</el-button>
    </div>

    <!-- 添加、批量删除按钮 -->
    <div class="add_Input">
      <el-button
        type="primary"
        @click="$router.push(`/hospital/ModifyHospital`)"
        >添加</el-button
      >
      <el-button
        type="dannger"
        :disabled="!idList.length"
        @click="GETbatchRemove"
        >批量删除</el-button
      >
    </div>

    <!-- 医院table -->
    <div class="hospital_Table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @select="removeAllHandler"
      >
        <el-table-column prop="date" type="selection" align="center" width="50">
        </el-table-column>
        <el-table-column type="index" label="序号" align="center" width="100">
        </el-table-column>
        <el-table-column
          prop="hosname"
          label="医院名称"
          align="center"
          width="210"
        >
        </el-table-column>
        <el-table-column
          prop="hoscode"
          label="医院编号"
          align="center"
          width="210"
        >
        </el-table-column>
        <el-table-column
          prop="apiUrl"
          label="api基础路径"
          align="center"
          width="210"
        >
        </el-table-column>
        <el-table-column prop="signKey" label="签名" align="center" width="250">
        </el-table-column>
        <el-table-column
          prop="contactsName"
          label="联系人姓名"
          align="center"
          width="250"
        >
        </el-table-column>
        <el-table-column
          prop="contactsPhone"
          label="联系人手机"
          align="center"
          width="220"
        >
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="120">
          <template v-slot="{ row }">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改医院"
              placement="top"
            >
              <el-button
                type="primary"
                title="修改医院"
                icon="el-icon-edit"
                size="mini"
                @click="$router.push(`/hospital/ModifyHospital/${row.id}`)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除医院"
              placement="top"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteHospital(row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 页码 -->
    <el-pagination
      class="page_Number"
      :current-page.sync="pages"
      :page-sizes="[5, 10, 15, 20]"
      :page-size.sync="limit"
      layout=" ->,total,  prev, pager, next, sizes,jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import {
  reqhospitalSet,
  reqRemove,
  reqSearch,
  reqbatchRemove,
} from "@/api/hospitalSet";
export default {
  data() {
    return {
      // 查询输入框输入的 名称\ 编号\ 结果
      hosname: "",
      hoscode: "",
      // 页码
      total: 0,
      pages: 1,
      limit: 5,
      tableData: [],
      idList: [],
    };
  },

  watch: {
    limit() {
      this.gethospitalSet();
    },
    pages() {
      this.gethospitalSet();
    },
  },

  mounted() {
    this.gethospitalSet();
  },
  methods: {
    // 点击添加按钮路由跳转到 添加医院页面
    addHospital() {
      this.$router.push("/hospital/ModifyHospital");
    },
    // 点击编辑按钮路由跳转到 修改医院设置页面 需要携带所有数据
    // modifyHospital() {
    //   this.$router.push("/hospital/ModifyHospital");

    // },

    // 获取医院设置列表数据
    async gethospitalSet() {
      try {
        const res = await reqhospitalSet(this.pages, this.limit);
        this.tableData = res.data.records;
        this.total = res.data.total;
        // console.log("医院设置列表数据", res);
      } catch (e) {}
    },

    // 删除医院设置
    async deleteHospital(id) {
      try {
        await reqRemove(id);
        this.gethospitalSet();
      } catch (e) {}
    },
    // 医院设置查询
    async Search() {
      try {
        const res = await reqSearch(
          this.page,
          this.limit,
          this.hosname,
          this.hoscode
        );
        // console.log(res);
        this.tableData = res.data.records;
        this.total = res.data.total;
      } catch (e) {}
    },
    // 清空输入框
    async remove() {
      this.hoscode = "";
      this.hosname = "";
      this.gethospitalSet();
    },

    removeAllHandler(data) {
      console.log(data);
      this.idList = data.map((item) => item.id);
      console.log(this.idList);
    },
    GETbatchRemove() {
      try {
        reqbatchRemove(this.idList);
        this.$message.success("批量删除成功");
        this.idList = [];
        this.gethospitalSet();
      } catch (error) {
        this.$message.error("批量删除失败");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  margin: 10px;
}
.handel_Input {
  display: flex;
}
.Input_name {
  width: 300px;
  margin-right: 20px;
}
.Input_number {
  width: 300px;
  margin-right: 20px;
}
.add_Input {
  margin-top: 20px;
}
.hospital_Table {
  margin-top: 20px;
}

.page_Number {
  margin-top: 20px;
}
</style>
